package com.blog.dao;

import com.blog.pojo.Comment;
import com.blog.pojo.Diary;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Mapper
@Repository("diaryDao")
public interface DiaryDao {

    @MapKey("id")
    @Select("select * from t_diary_motto")
    ArrayList<HashMap<String,String>> findDiaryMotto();

    @Select("<script>" +
            "select count(*) from t_diary" +
            "<where>" +
            "<if test='value!=\"\"'>" +
            "createTime >= #{createTime}" +
            "</if>" +
            "</where>" +
            "</script>"
            )
    int findDiaryCount(String createTime);

    @Select("<script>" +
            "select * from t_diary" +
            "<where>" +
            "<if test='createTime!=\"\"'>" +
            "createTime >= #{createTime}" +
            "</if>" +
            "</where>" +
            "order by createTime ASC limit #{startIndex},10" +
            "</script>" )
    ArrayList<Diary> findDiary(@Param("startIndex") int startIndex, @Param("createTime")String createTime);

    @Select("select * from t_diary_comment where d_id = #{id}")
    List<Comment> findDiaryComment(int id);

    @Select("select count(*) from t_diary_comment where d_id = #{id}")
    int findDiaryCommentCountById(int id);

    //评论
    @Insert("insert into t_diary_comment values(null,#{id},#{comment.time},#{comment.content},0)")
    void addComment(@Param("id") int id, @Param("comment") Comment comment);

    //添加说说
    @Insert("insert into t_diary values(null,#{title},#{createTime},#{content})")
    void addDiary(Diary diary);
}
